package com.monkeyk.os.web.api;


import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by yangkai on 2017/3/7.
 */
public class HighsunPropertyClient {
    protected static String JSONSUCCESS(HttpServletResponse response, SysCode code, JSONObject text) {
        try {
            response.setContentType("application/json; charset=UTF-8");
            if(code.getCode().equals("3")){
                response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
            }else {
                response.setStatus(HttpServletResponse.SC_OK);
            }
            /*if(code.getCode().equals("2")||code.getCode().equals("5")){
                response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
            }else if(code.getCode().equals("0")){
                response.setStatus(HttpServletResponse.SC_OK);
            }else if(code.getCode().equals("4")){
                response.setStatus(HttpServletResponse.SC_FORBIDDEN);
            } else if(code.getCode().equals("1")){
                response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            } else if(code.getCode().equals("3")){
                response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
            }*/
            response.getWriter().write(getResponseinterFaceBody(code, text));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    protected static String getResponseinterFaceBody(SysCode code, JSONObject result) {
        JSONObject body = new JSONObject();
        body.put("code", Integer.parseInt(code.getCode()));
        body.put("msg", code.getMessage());
        body.put("data", result);
        return  JSONObject.toJSONString(body, SerializerFeature.WriteMapNullValue);
    }
    protected static String getFullPath(HttpServletRequest request) {
        StringBuffer sb = new StringBuffer();
        sb.append(request.getScheme()).append("://").append(request.getServerName()).append(":").append(request.getServerPort());
        if (StringUtils.isNotEmpty(request.getContextPath())) {
            sb.append(request.getContextPath());
        }
        return sb.toString();
    }


}
